//
//  BBuyNowShopStoreVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/10.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BBuyNowShopStoreVC.h"
#import "BShopPartModel.h"
#import "BBuyNowShopCell.h"
#import "BBuyNowShopStoreHeaderView.h"

#import "BBuyNowShopDetailVC.h"
//配件排序类型
typedef NS_ENUM(NSUInteger, PartSortType) {
    PartSortTypeSynthesize = 0,      //综合
    PartSortTypeSales                //销量
};
@interface BBuyNowShopStoreVC () <UITableViewDelegate,UITableViewDataSource,BBuyNowShopStoreHeaderViewDeletate>
{
    BOOL _isRequesting;//是否正在请求数据
    PartSortType _dataType;//排序类型
}
@property (nonatomic, strong) UIImageView *ivStore;//店铺图片
@property (nonatomic, strong) UILabel *lblName;//店铺名称
@property (nonatomic, strong) UILabel *lblDetail;//店铺简介
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *partArray;//数据源
@property (nonatomic, assign) NSInteger pageNumber;//分页
@end

@implementation BBuyNowShopStoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"进店逛逛";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = kColorWithHex(0xffffff);
    
    _partArray = [NSMutableArray array];
    _dataType = PartSortTypeSynthesize;
    
    [self setupViewAction];
}
#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xffffff);
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 300;
    
    [_tableView registerClass:[BBuyNowShopCell class] forCellReuseIdentifier:NSStringFromClass([BBuyNowShopCell class])];
    
    UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 90)];
    _ivStore = [[UIImageView alloc] initWithFrame:CGRectMake(26, 16, 40, 40)];
    [viewHeader addSubview:_ivStore];
    _lblName = [[UILabel alloc] initWithFrame:CGRectMake(86, 16, kScreen_Width - 112, 20)];
    _lblName.font = [UIFont systemFontOfSize:14];
    _lblName.textColor = kColorWithHex(0x666666);
    [viewHeader addSubview:_lblName];
    _lblDetail = [UILabel new];
    _lblDetail.preferredMaxLayoutWidth = kScreen_Width - 112;
    _lblDetail.font = [UIFont systemFontOfSize:12];
    _lblDetail.textColor = kColorWithHex(0x666666);
    _lblDetail.numberOfLines = 2;
    [viewHeader addSubview:_lblDetail];
    [_lblDetail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblName.mas_bottom).offset(7);
        make.leading.trailing.equalTo(self.lblName);
    }];
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, 89, kScreen_Width, 1)];
    viewLine.backgroundColor = kColorWithHex(0xf2f2f2);
    [viewHeader addSubview:viewLine];
    
    _tableView.tableHeaderView = viewHeader;
    
    [self updateStoreInfo];
    
    kWeakSelf;
    //下拉刷新
    MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageNumber = 1;
        [weakSelf requestData];
    }];
    mj_header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.mj_header = mj_header;
    
    //上拉加载
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf requestData];
    }];
    
    [_tableView.mj_header beginRefreshing];
}
#pragma mark - 更新店铺数据
- (void)updateStoreInfo{
    if (self.storeModel) {
        [HFTools imageViewUpdateWithUrl:self.storeModel.factory.factory_img withImageView:_ivStore withPlaceholderImage:@""];
        _lblName.text = self.storeModel.factory.factory_name;
        _lblDetail.text = self.storeModel.factory.factory_remarks;
        return;
    }
    if (self.orderModel) {
        [HFTools imageViewUpdateWithUrl:self.orderModel.factory_img withImageView:_ivStore withPlaceholderImage:@""];
        _lblName.text = self.orderModel.factory_name;
        _lblDetail.text = self.orderModel.factory_remarks;
        return;
    }
    if (self.orderListModel) {
        [HFTools imageViewUpdateWithUrl:self.orderListModel.factory_img withImageView:_ivStore withPlaceholderImage:@""];
        _lblName.text = self.orderListModel.factory_name;
        _lblDetail.text = self.orderListModel.factory_remarks;
        return;
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.partArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BBuyNowShopCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BBuyNowShopCell class]) forIndexPath:indexPath];
    BShopPartModel *model = [self.partArray objectAtIndexSafe:indexPath.row];
    cell.model = model;
    return cell;
}
#pragma mark - UITableViewDelegate
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BBuyNowShopStoreHeaderView *viewHeader = [[BBuyNowShopStoreHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 32)];
    viewHeader.delegate = self;
    return viewHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BShopPartModel *model = [self.partArray objectAtIndexSafe:indexPath.row];
    BBuyNowShopDetailVC *vc = [BBuyNowShopDetailVC new];
    vc.shopID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - BBuyNowShopStoreHeaderViewDeletate
- (void)headerViewBtnClick:(NSInteger)tag{
    if (tag == 200) {
        //综合
        _dataType = PartSortTypeSynthesize;
    }else if (tag == 201){
        //销量
        _dataType = PartSortTypeSales;
    }
    //更新数据
    _pageNumber = 1;
    [self requestData];
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf;
    if (_isRequesting) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        return;
    }
    _isRequesting = YES;
    [MBProgressHUD showHUDAddedTo:self.view msg:@"数据加载中" animated:YES];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObjectIfNotNil:self.storeID forKey:@"id"];//商铺ID
    [dict setObjectIfNotNil:@(_dataType) forKey:@"sale"];//分类1销量 0综合
    [NetWorkTool POST:partStoreShops param:dict success:^(id dic) {
        [weakSelf requestResult];
        
        NSArray *dataArr = [dic objectForKeyNotNil:@"data"];
        [dataArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BShopPartModel *model = [BShopPartModel mj_objectWithKeyValues:obj];
            [weakSelf.partArray addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
        //weakSelf.pageNumber++;//暂时没有分页
        
    } other:^(id dic) {
        [weakSelf requestResult];
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        [weakSelf requestResult];
    } needUser:YES];
}
- (void)requestResult{
    _isRequesting = NO;
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if (self.pageNumber == 1) {
        [self.partArray removeAllObjects];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
